<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户账号" prop="userName">-->
<!--        <el-input-->
<!--          v-model="queryParams.userName"-->
<!--          placeholder="请输入用户账号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="用户邮箱" prop="email">-->
<!--        <el-input-->
<!--          v-model="queryParams.email"-->
<!--          placeholder="请输入用户邮箱"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="手机号码" prop="phonenumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.phonenumber"-->
<!--          placeholder="请输入手机号码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="头像地址" prop="image">-->
<!--        <el-input-->
<!--          v-model="queryParams.image"-->
<!--          placeholder="请输入头像地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="密码" prop="password">-->
<!--        <el-input-->
<!--          v-model="queryParams.password"-->
<!--          placeholder="请输入密码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后登录IP" prop="loginIp">-->
<!--        <el-input-->
<!--          v-model="queryParams.loginIp"-->
<!--          placeholder="请输入最后登录IP"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后登录时间" prop="loginDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.loginDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择最后登录时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="userId" />
      <el-table-column label="部门名称" align="center" prop="dept.deptName" />
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
<!--      <el-table-column label="用户类型" align="center" prop="userType" />-->
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="用户性别" align="center" prop="sex" >
        <template slot-scope="scope">
          {{ String(scope.row.sex) === '1' ? '女' : '男' }}
        </template>
      </el-table-column>>
      <el-table-column label="头像地址" align="center" prop="avatar" >
        <template slot-scope="scope">
          <el-image
            :src="getMinioImageUrl(scope.row.avatar)"
            fit="scale-down"
            :preview-src-list="[getMinioImageUrl(scope.row.avatar)]">
          </el-image>
        </template>
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>-->
<!--        </template>-->
      </el-table-column>
<!--      <el-table-column label="密码" align="center" prop="password" />-->
      <el-table-column label="账号状态" align="center" prop="status" >
        <template slot-scope="scope">
          {{ String(scope.row.status) === '1' ? '停用' : '正常' }}
        </template>
      </el-table-column>>
<!--      </el-table-column>>-->
<!--      <el-table-column label="最后登录IP" align="center" prop="loginIp" />-->
<!--      <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门名称" prop="dept.deptName">
          <el-input v-model="form.dept.deptName" placeholder="请输入部门名称" >
<!--            <i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--            <el-dropdown-menu slot="dropdown">-->
<!--              <el-dropdown-item>黄金糕</el-dropdown-item>-->
<!--              <el-dropdown-item>狮子头</el-dropdown-item>-->
<!--              <el-dropdown-item>螺蛳粉</el-dropdown-item>-->
<!--              <el-dropdown-item disabled>双皮奶</el-dropdown-item>-->
<!--              <el-dropdown-item divided>蚵仔煎</el-dropdown-item>-->
<!--            </el-dropdown-menu>-->
          </el-input>
<!--          <el-checkbox-group v-model="form.dept" @change="handleCheckedCitiesChange">-->
<!--            <el-checkbox v-for="d in dept" :label="d.deptName" :key="d.deptName">{{d.deptName}}</el-checkbox>-->
<!--          </el-checkbox-group>-->
<!--          <el-dropdown>-->
<!--  <span class="el-dropdown-link">-->
<!--    下拉菜单<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--  </span>-->
<!--            <el-dropdown-menu slot="dropdown">-->
<!--              <el-dropdown-item>黄金糕</el-dropdown-item>-->
<!--              <el-dropdown-item>狮子头</el-dropdown-item>-->
<!--              <el-dropdown-item>螺蛳粉</el-dropdown-item>-->
<!--              <el-dropdown-item disabled>双皮奶</el-dropdown-item>-->
<!--              <el-dropdown-item divided>蚵仔煎</el-dropdown-item>-->
<!--            </el-dropdown-menu>-->
<!--          </el-dropdown>-->
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="头像地址" prop="avatar">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/common/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.avatar" :src="'http://localhost:9000/zfr/' + form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
<!--          <image-upload v-model="form.avatar" placeholder="请输入头像地址" />-->
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>

<!--        <el-form-item label="删除标志" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="最后登录IP" prop="loginIp">-->
<!--          <el-input v-model="form.loginIp" placeholder="请输入最后登录IP" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="最后登录时间" prop="loginDate">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.loginDate"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择最后登录时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/user/user"
import imagePreview from '@/components/ImagePreview/index.vue';
export default {
  name: "User",
  data() {
    return {
      // MinIO 配置
      minioConfig: {
        endpoint: 'http://127.0.0.1:9000/',  // MinIO 服务器地址
        bucketName: 'zfr',           // 存储桶名称
        accessKey: 'adminminio',           // 访问密钥
        // secretKey: 'adminminio'            // 秘密密钥（前端不应暴露，这里仅作示例，实际应通过后端签名）
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        loginIp: null,
        loginDate: null,
      },
      // 表单参数
      form: {
        dept:{}
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  computed: {
    // MinIO 图片访问URL前缀
    minioImagePrefix() {
      return `${this.minioConfig.endpoint}/${this.minioConfig.bucketName}/`;
    },
    // MinIO 上传URL
    minioUploadUrl() {
      // 实际项目中应调用后端接口获取上传URL或签名

      return `${this.minioConfig.endpoint}/${this.minioConfig.bucketName}`;
    },
    // MinIO 上传请求头（如果需要）
    minioHeaders() {
      // 注意：不要在前端暴露secretKey，应通过后端获取临时凭证
      return {};
    }
  },
  created() {
    this.getList()
  },
  components: {
    imagePreview
  },
  methods: {
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.dept.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.dept.length;
      this.form.dept = value
      console.log(value)
      console.log("-------")
      console.log(this.form.dept)
      // let checkedCount = value.length;
      // this.checkAll = checkedCount === this.cities.length;
      // this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },
    handleAvatarSuccess(res, file) {
      console.log(res.msg)
      console.log(555)
      this.form.avatar = res.msg;
    },
    // beforeAvatarUpload(file) {
    //   // const isJPG = file.type === 'image/jpeg';
    //   // const isLt2M = file.size / 1024 / 1024 < 2;
    //
    //   if (!isJPG) {
    //     this.$message.error('上传头像图片只能是 JPG 格式!');
    //   }
    //   if (!isLt2M) {
    //     this.$message.error('上传头像图片大小不能超过 2MB!');
    //   }
    //   return isJPG && isLt2M;
    // },
    // // 生成MinIO图片URL
    getMinioImageUrl(imageName) {
      const url = `${this.minioConfig.endpoint}${this.minioConfig.bucketName}/${imageName}`;
      console.log('[图片URL]', imageName, '→', url); // 打印原始文件名和生成的URL
      console.log(777)
      console.log(imageName)
      console.log(url)
      return url;
    },
    // getMinioImageUrl(imageName) {
    //   // if (!imageName) return 'https://picsum.photos/100/100'; // 默认占位图
    //
    //   // 如果是完整URL则直接返回
    //   if (imageName.startsWith('http')) return imageName;
    //
    //   // 拼接MinIO服务器URL
    //   return `${this.minioImagePrefix}${imageName}`;
    // },

    // 自定义上传方法（如果需要）
    customUpload(file) {
      // 这里可以实现自定义上传逻辑，例如获取签名、处理进度等
      // 实际项目中建议通过后端服务处理MinIO上传
      return new Promise((resolve, reject) => {
        // 示例：使用axios上传到MinIO
        // axios.post(this.minioUploadUrl, file, {
        //   headers: this.minioHeaders
        // })
        // .then(response => {
        //   resolve(response.data);
        // })
        // .catch(error => {
        //   reject(error);
        // });

        // 由于安全限制，这里简化处理，实际项目中应通过后端上传
        resolve(file.name);
      });
    },
    // //性别
    // getSexText(sexs){
    //   const  map={
    //     0: '男',
    //     1: '女'
    //   };
    //   return map[sex] || '未知性别';
    // },
    /** 查询用户信息列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        dept:{},
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        delFlag: null,
        loginIp: null,
        loginDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加用户信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        console.log("-------")
        console.log(response)
        this.form = response.data
        this.open = true
        this.title = "修改用户信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$modal.confirm('是否确认删除用户信息编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>

